package com.CST.service.impl.zx;

import cn.hutool.core.bean.BeanUtil;
import com.CST.bo.zx.ProjectAppealBO;
import com.CST.bo.zx.ProjectAppealDetailBO;
import com.CST.bo.zx.ProjectAppealHandleBO;
import com.CST.entity.cb.BdSysUser;
import com.CST.entity.zx.ZxProjectAppeal;
import com.CST.mapper.cst.cb.ZxProjectAppealMapper;
import com.CST.service.cb.IBdSysUserService;
import com.CST.service.zx.IZX_ProjectInfoService;
import com.CST.service.zx.IZxProjectAppealService;
import com.CST.util.HttpUtil;
import com.CST.vo.zx.ProjectVo;
import com.CST.vo.zx.ZxProjectAppealInfo;
import com.CST.vo.zx.ZxProjectBaseInfo;
import com.CST.vo.zx.ZxProjectHandingResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2023-03-23
 */
@Service
public class ZxProjectAppealServiceImpl extends ServiceImpl<ZxProjectAppealMapper, ZxProjectAppeal> implements IZxProjectAppealService {

    @Resource
    private ZxProjectAppealMapper zxProjectAppealMapper;
    @Resource
    private IBdSysUserService bdSysUserService;
    @Resource
    private IZX_ProjectInfoService zxProjectInfoService;

    @Override
    public boolean saveZxProjectAppeal(ZxProjectAppeal zxProjectAppeal) {
        String userId = HttpUtil.getRequestHeaderInfo("userId");
        BdSysUser user = bdSysUserService.getUserById(userId);
        zxProjectAppeal.setCreateUserId(userId);
        zxProjectAppeal.setCreateUserName(user.getRealName());
        zxProjectAppeal.setCreateTime(LocalDateTime.now());
        return this.save(zxProjectAppeal);
    }

    @Override
    public List<ZxProjectAppeal> queryAllAppeal(ProjectAppealBO projectAppealBO) {
        String userId = HttpUtil.getRequestHeaderInfo("userId");
        LambdaQueryWrapper<ZxProjectAppeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(zxProjectAppealLambdaQueryWrapper -> zxProjectAppealLambdaQueryWrapper
                .like(projectAppealBO.getProjectCode() != null, ZxProjectAppeal::getProjectCode, projectAppealBO.getProjectCode())
                .like(projectAppealBO.getProjectEasyName() != null, ZxProjectAppeal::getProjectEasyName, projectAppealBO.getProjectEasyName())
                .eq(projectAppealBO.getStatus() != null, ZxProjectAppeal::getStatus, projectAppealBO.getStatus())
                .eq(userId!=null,ZxProjectAppeal::getCreateUserId,userId)
                .apply(projectAppealBO.getCreateDate() != null, "convert(char(10) ,create_time , 120)='" + projectAppealBO.getCreateDate() + "'")
        ).and(zxProjectAppealLambdaQueryWrapper -> zxProjectAppealLambdaQueryWrapper
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountCode, projectAppealBO.getSelectAll())
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountNum, projectAppealBO.getSelectAll())
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountName, projectAppealBO.getSelectAll())
                .apply(projectAppealBO.getSelectAll() == null,"1=1"));
        List<ZxProjectAppeal> list = this.list(lambdaQueryWrapper);
        return list;
    }

    @Override
    public ZxProjectAppealInfo queryAppealDetail(ProjectAppealDetailBO projectAppealDetailBO) {
        return zxProjectAppealMapper.queryAppealDetail(projectAppealDetailBO);
    }

    @Override
    public ZxProjectHandingResult queryHandingResult(ProjectAppealDetailBO projectAppealDetailBO) {
//        LambdaQueryWrapper<ZxProjectAppeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(projectAppealDetailBO.getId()!=null,ZxProjectAppeal::getId, projectAppealDetailBO.getId())
//                .select(ZxProjectAppeal::getStatus,ZxProjectAppeal::getUserName,ZxProjectAppeal::getUpdateTime,ZxProjectAppeal::getHandlingOpinions);
//        ZxProjectAppeal one = this.getOne(lambdaQueryWrapper);
//        ZxProjectHandingResult zxProjectHandingResult = new ZxProjectHandingResult();
//        BeanUtil.copyProperties(one,zxProjectHandingResult);
        ZxProjectHandingResult zxProjectHandingResult = zxProjectAppealMapper.queryHandingResult(projectAppealDetailBO);
        return zxProjectHandingResult;
    }

    @Override
    public Boolean editZxProjectAppeal(ZxProjectAppeal zxProjectAppeal) {
        LambdaUpdateWrapper<ZxProjectAppeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(zxProjectAppeal.getId()!= null,ZxProjectAppeal::getId,zxProjectAppeal.getId())
                .eq( ZxProjectAppeal::getStatus,0)
                .set(zxProjectAppeal.getRemarks()!=null,ZxProjectAppeal::getRemarks,zxProjectAppeal.getRemarks());
        return this.update(lambdaUpdateWrapper);
    }

    @Override
    public List<ZxProjectAppeal> queryAlleHandleAppeal(ProjectAppealBO projectAppealBO) {
        LambdaQueryWrapper<ZxProjectAppeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.and(zxProjectAppealLambdaQueryWrapper -> zxProjectAppealLambdaQueryWrapper
                .like(projectAppealBO.getProjectCode() != null, ZxProjectAppeal::getProjectCode, projectAppealBO.getProjectCode())
                .like(projectAppealBO.getProjectEasyName() != null, ZxProjectAppeal::getProjectEasyName, projectAppealBO.getProjectEasyName())
                .like(projectAppealBO.getCreateName()!=null,ZxProjectAppeal::getCreateUserName,projectAppealBO.getCreateName())
                .eq(projectAppealBO.getStatus() != null, ZxProjectAppeal::getStatus, projectAppealBO.getStatus())
                .apply(projectAppealBO.getCreateDate() != null, "convert(char(10) ,create_time , 120)='" + projectAppealBO.getCreateDate() + "'")
                .apply("1=1")
        ).and(zxProjectAppealLambdaQueryWrapper -> zxProjectAppealLambdaQueryWrapper
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountCode, projectAppealBO.getSelectAll())
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountNum, projectAppealBO.getSelectAll())
                .or()
                .like(projectAppealBO.getSelectAll() != null, ZxProjectAppeal::getAccountName, projectAppealBO.getSelectAll())
                .apply(projectAppealBO.getSelectAll() == null,"1=1"));
        List<ZxProjectAppeal> list = this.list(lambdaQueryWrapper);
        return list;
    }

    @Override
    public Boolean alreadyEditForZxProjectAppeal(ProjectAppealHandleBO projectAppealHandleBO) {
        String userId = HttpUtil.getRequestHeaderInfo("userId");
        BdSysUser user = bdSysUserService.getUserById(userId);
        LambdaUpdateWrapper<ZxProjectAppeal> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(projectAppealHandleBO.getId()!=null, ZxProjectAppeal::getId,projectAppealHandleBO.getId())
                .eq(ZxProjectAppeal::getStatus,0)
                .set(ZxProjectAppeal::getUserId,user.getId())
                .set(ZxProjectAppeal::getUserName,user.getRealName())
                .set(projectAppealHandleBO.getHandlingOpinions()!=null,ZxProjectAppeal::getHandlingOpinions,projectAppealHandleBO.getHandlingOpinions())
                .set(projectAppealHandleBO.getStatus()!=null,ZxProjectAppeal::getStatus, projectAppealHandleBO.getStatus())
                .set(ZxProjectAppeal::getUpdateTime,LocalDateTime.now());
        return this.update(lambdaUpdateWrapper);
    }

    @Override
    public int getNoHandleNum() {
        LambdaQueryWrapper<ZxProjectAppeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ZxProjectAppeal::getStatus,0);
        return this.count(lambdaQueryWrapper);
    }

    @Override
    public Map<String, Object> queryZxProjectAppealForDetail(ProjectAppealDetailBO projectAppealDetailBO) {
        ProjectVo projectDetail = zxProjectInfoService.getProjectDetail(projectAppealDetailBO.getFinanceCode());
        ZxProjectBaseInfo zxProjectBaseInfo = new ZxProjectBaseInfo();
        BeanUtil.copyProperties(projectDetail,zxProjectBaseInfo);
        LambdaQueryWrapper<BdSysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BdSysUser::getUserName,projectDetail.getUserId());
        BdSysUser user = bdSysUserService.getOne(lambdaQueryWrapper);
        zxProjectBaseInfo.setDeptName(user.getDeptName());
        ZxProjectAppealInfo zxProjectAppealInfo = this.queryAppealDetail(projectAppealDetailBO);
        ZxProjectHandingResult zxProjectHandingResult = this.queryHandingResult(projectAppealDetailBO);
        Map<String,Object> map = new HashMap<>();
        map.put("zxProjectBaseInfo",zxProjectBaseInfo);
        map.put("zxProjectAppealInfo",zxProjectAppealInfo);
        map.put("zxProjectHandingResult",zxProjectHandingResult);
        return map;
    }
}
